from sqlalchemy import (
    Column,
    Integer,
    String,
    BigInteger,
    Text,
    Boolean,
    ForeignKey,
)
from sqlalchemy.orm import relationship
from database.database import Base



class GenColumn(Base):
    __tablename__ = "gen_column"

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    name = Column(String(64), nullable=False, comment="列名称")
    comment = Column(String(256), comment="列描述")
    type = Column(String(32), nullable=False, default="String", comment="SQLA 模型列类型")
    pd_type = Column(String(32), nullable=False, default="str", comment="列类型对应的 pydantic 类型")
    default = Column(Text, comment="列默认值")
    sort = Column(Integer, comment="列排序")
    length = Column(Integer, nullable=False, default=0, comment="列长度")
    is_pk = Column(Boolean, nullable=False, default=False, comment="是否主键")
    is_nullable = Column(Boolean, nullable=False, default=False, comment="是否可为空")
    gen_business_id = Column(
        BigInteger,
        ForeignKey("gen_business.id", ondelete="CASCADE"),
        nullable=False,
        comment="代码生成业务ID",
    )

    business = relationship("GenBusiness", back_populates="columns")